use "/Users/ddgueorg/Dropbox/Indonesia/Elections Paper/Drafts/2018 draft/PS submissions/Resubmission/PS replication/Indonesia_Rematch_replication.dta", clear
/* Mapping */
set more off 
preserve
keep if level==4
collapse (mean) dist_jokowi_share, by (id)
merge m:m id using "unique_id.dta"
drop _merge
*label define winner 1 "Jokowi" 0 "Prabowo"
spmap dist_jokowi_share using "indo_pemilu_coor.dta", id(id) clnumber(5) ///* Black and White
fcolor(Greys2) osize(vthin) title(, size(0)) legend(title("Joko-Kalla Share of Vote", size(*0.5)))
spmap dist_jokowi_share using "indo_pemilu_coor.dta", id(id) clnumber(5) ///* Color
fcolor(Reds) osize(vthin) title(, size(0)) legend(title("Joko-Kalla Share of Vote", size(*0.5)))
restore

*** Village level turnout ****
g village_turnout=(sah/pop)
replace village_turnout=. if sah>pop
sum village_turnout pov_level pov_depth pov_severity pov_gini

**Table 1**
tab level 
egen temp=group(prov_name reg_name dist_name vil_name)
sum prov_total reg_total dist_total vil_total sah prov_jokowi_share reg_jokowi_share dist_jokowi_share vil_jokowi_share ps_jokowi_share if drop!=1

sutex dist_turnout_share dist_ur_urban_share dist_pov_level ///
dist_rel_muslim_share dist_rel_christian_share dist_rel_catholic_share dist_rel_buddhist_share dist_rel_hindu_share dist_rel_kong_hu_chu_share ///
dist_emp* ///
dist_edu* ///
dist_wat* if level==3

/*** Begin District Analysis */
*keep dist_rel_muslim_share dist_div_f_diff dist_lit_f_diff dist_edu_f_senior dist_fert_f_10_19 dist_pov_level dist_ur_rural_share dist_wat_dwe dist_int_no_share
collapse prov_total - temp, by(prov_name reg_name dist_name)

*Factor Analysis*
pca dist_rel_muslim_share dist_div_f_diff dist_lit_f_diff dist_edu_f_senior dist_fert_f_10_19 dist_pov_level dist_ur_rural_share dist_wat_dwe dist_int_no_share
screeplot
factor dist_rel_muslim_share dist_div_f_diff dist_lit_f_diff dist_edu_f_senior dist_fert_f_10_19 dist_pov_level dist_ur_rural_share dist_wat_dwe dist_int_no_share, factors(3)
rotate, varimax blanks(.3)
predict rural poor conservative
sum rural 
replace rural = (rural - `r(min)') / (`r(max)'-`r(min)')
replace rural=0 if rural<0
sum poor 
replace poor = (poor - `r(min)') / (`r(max)'-`r(min)')
replace poor=0 if poor<0
sum conservative 
replace conservative = (conservative - `r(min)') / (`r(max)'-`r(min)')
replace conservative=0 if conservative<0

label variable conservative "Conservative"
label variable poor "Poor"
label variable rural "Rural"
label variable ln_dist_pop "Population (log)"
label variable  minority_tile "Minority (decile)"

** Merging Islamic Proxies ** 
merge m:m prov_name using "hajj.dta", update
drop if _merge==2
drop _merge
merge m:m prov_name using "leg_elections_islamist.dta", update
drop _merge

********** Proxying for Conservativism  ************

*hajj pilgrimages*
g hajj_share=hajj/prov_pop
g ln_hajj_share=ln(hajj_share)

***Figures***
twoway (lpolyci dist_turnout_share conservative if dist_turnout>.4)  (scatter dist_turnout_share conservative if dist_turnout>.4, msize(vtiny) mcolor(black) msymbol(circle_hollow)) , ///
ytitle(Turnout (censored)) xtitle(Conservative Factor) legend(off) xlabel(0"Low" 1 "High") name(Conservative_turnout, replace)

twoway (lpolyci dist_turnout_share dist_jokowi_share if dist_turnout>.4 & dist_jokowi_share>.05)  (scatter dist_turnout_share dist_jokowi_share if dist_turnout>.4, msize(vtiny) mcolor(black) msymbol(circle_hollow)), ///
xtitle(Jokowi Vote Share) ytitle(Turnout (censored)) legend(off) xscale(range(.2 1)) xlabel(#6) name(Minority_Turnout_Jokowi, replace) scheme(s1mono)

graph combine Conservative_turnout Minority_Turnout_Jokowi, rows(1) xsize(10) ysize(5)
graph export "Conservative_minority_turnout_jokowi.eps", as(eps) preview(off) replace

***Figures color***
twoway (lpolyci dist_turnout_share conservative if dist_turnout>.4)  (scatter dist_turnout_share conservative if dist_turnout>.4 & dist_rel_muslim_share<.5, msize(vtiny) msymbol(circle_hollow) mcolor(blue)) ///
(scatter dist_turnout_share  conservative if dist_turnout>.4 & dist_rel_muslim_share>.49, msize(tiny) msymbol(triangle) mcolor(maroon)), ///
ytitle(Turnout (censored)) xtitle(Conservative Factor) legend(off) xlabel(0"Low" 1 "High") name(Conservative_turnout, replace)

twoway (lpolyci dist_turnout_share dist_jokowi_share if dist_turnout>.4 & dist_jokowi_share>.05)  (scatter dist_turnout_share dist_jokowi_share if dist_rel_muslim_share<.5 & dist_turnout>.4, msize(vtiny) msymbol(circle_hollow) mcolor(blue)) ///
(scatter dist_turnout_share dist_jokowi_share if dist_rel_muslim_share>.49 & dist_turnout>.4, msize(tiny) msymbol(triangle) mcolor(maroon)), ///
xtitle(Jokowi Vote Share) ytitle(Turnout (censored)) legend(off) xscale(range(.2 1)) xlabel(#6) name(Minority_Turnout_Jokowi, replace) 

graph combine Conservative_turnout Minority_Turnout_Jokowi, rows(1) xsize(10) ysize(5)
graph export "Conservative_minority_turnout_jokowi_color.eps", as(eps) preview(off) replace

***Regressions***
set more off
/*%%%%%%%%%%%*/

*Main Table*
set more off
*model 1 - cleavages*
*eststo: reg dist_jokowi_share c.minority_tile , r
*model 2 - controls*
eststo: reg dist_jokowi_share conservative poor rural , r
*model 3 - province*
eststo: reg dist_jokowi_share conservative poor rural muslim_tile dist_emp* , r
*model 4 - minority*
eststo: reg dist_jokowi_share conservative poor rural muslim_tile ln_dist_pop dist_emp* i.prov_id, r
*model 5 - minority*
*eststo: reg dist_turnout c.minority_tile , r
*model 6 - cleavages*
eststo: reg dist_turnout conservative poor rural , r
*model 7 - controls*
eststo: reg dist_turnout conservative poor rural muslim_tile dist_emp* , r
*model 8 - province*
eststo: reg dist_turnout conservative poor rural muslim_tile ln_dist_pop dist_emp* i.prov_id , r
esttab using indo_2014_main_results.tex, b(%9.3fc) label replace ///
compress se r2 title(Support and Turnout for the Joko-Kalla Ticket \label{tab:main_regression}) ///
mgroups("Joko-Kalla Vote Share" "District Turnout", pattern(1 0 0 1 0 0) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
mtitles("Cleavages " "Controls" "Province Fe" "Cleavages " "Controls" "Province Fe") ///
 star(* 0.10 ** 0.05 *** 0.001) ///
 order (conservative poor rural muslim_tile ln_dist_pop Economic_Controls Provincial_FE) drop( dist_emp* *.prov_id)
eststo: reg dist_turnout conservative poor rural minority_tile ln_dist_pop dist_emp* i.prov_id , r
esttab using indo_2014_main_results.csv, b(%9.3fc) label replace ///
compress se r2 title(Support and Turnout for the Joko-Kalla Ticket \label{tab:main_regression}) ///
mgroups("Joko-Kalla Vote Share" "District Turnout", pattern(1 0 0 1 0 0) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
mtitles("Cleavages " "Controls" "Province Fe" "Cleavages " "Controls" "Province Fe") ///
 star(* 0.10 ** 0.05 *** 0.001) ///
 order (conservative poor rural muslim_tile ln_dist_pop Economic_Controls Provincial_FE) drop( dist_emp* *.prov_id)
eststo clear 
